package Boundary;
import Entity.ModulesGrades;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.*;
import java.util.List;


import static Control.Control.Add_Component;
import static Control.Control.json1;

/**
 *Title       :ModulesManagement.java
 *Description: This class initialize GUI of ModulesManagement
 * @author Group77
 * @version 4.0
 * @description display an operational page for modules
 */
public class ModulesManagement extends Overall{
    public ModulesManagement(int number){
        super("Modules Management");

        //Return
        panel1.setLayout(new BorderLayout());
        panel1.setBorder(BorderFactory.createEmptyBorder());
        JButton button0 = new JButton("   Go back   ");
        button0.setBorder(BorderFactory.createRaisedBevelBorder());
        button0.setBackground(Color.cyan);
        button0.setFont(new Font("STSong", Font.BOLD, 25));
        JLabel label1 = new JLabel("Module Grades  ", JLabel.CENTER);
        label1.setFont(new Font("TimesRoman", Font.ITALIC, 25));
        JPanel empty1 = new JPanel();
        empty1.setPreferredSize(new Dimension(100, 10));
        panel1.add(button0, BorderLayout.WEST);
        panel1.add(empty1, BorderLayout.NORTH);
        panel1.add(label1, BorderLayout.EAST);
        Add_Component(jfr, gbl, panel1, gbc, 0, 0, 1, 1, 1, 0);


        button0.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        jfr.setVisible(false);
                        new Start(number);
                        jfr.dispose();
                    }
                }
        );


        panel3.setLayout(new BorderLayout());
        panel3.setBorder(BorderFactory.createEmptyBorder());
        Object[] columnNames = {"Name", "Module Number", "Semester", "Credit", "Grade"};
        List<ModulesGrades> modules=json1.readArray(ModulesGrades.class);

        List list = new ArrayList();
        int i = 0;
        while(i < modules.size()){
            if(number == modules.get(i).getStudentsNumber()){
                String[] str = {modules.get(i).getModuleName(),modules.get(i).getModuleNumber(),modules.get(i).getSemester(),modules.get(i).getCredits(),modules.get(i).getGrades()};
                list.add(str);
            }
            i++;
        }
        Object[] s = (Object[])list.toArray();
        String[][] rowData = new String[s.length][];
        for(int j=0;j<s.length;j++){
            rowData[j] = (String[])s[j];
        }

        //calculate average
        int grade_average=0;
        int allgrades=0;
        int allcredits=0;
        i=0;

        while (i<modules.size()){
            if(number==modules.get(i).getStudentsNumber()){
            allgrades=allgrades+(Integer.parseInt(modules.get(i).getGrades())*Integer.parseInt(modules.get(i).getCredits()));
            }
            i++;
        }
        i=0;
        while(i<modules.size()){
            if(number==modules.get(i).getStudentsNumber()){
                allcredits=allcredits+(Integer.parseInt(modules.get(i).getCredits()));
            }
            i++;
        }

        DecimalFormat dF = new DecimalFormat("0.00");
        String averagenumber=dF.format((float)allgrades/allcredits);

        String gpanumber= dF.format(4-(float)3*(100-((float)allgrades/allcredits))*(100-((float)allgrades/allcredits))/1600);


        JTable table1 = new JTable (rowData, columnNames);
        table1.setForeground(Color.BLACK);
        table1.setFont(new Font(null, Font.PLAIN, 14));
        table1.setSelectionForeground(Color.DARK_GRAY);
        table1.setSelectionBackground(Color.cyan);
        table1.setGridColor(Color.GRAY);
        table1.setBackground(Color.white);
        table1.getTableHeader().setFont(new Font(null, Font.BOLD, 14));
        table1.getTableHeader().setForeground(Color.BLACK);
        table1.getTableHeader().setBackground(new Color(113,191,234));
        table1.getTableHeader().setResizingAllowed(false);
        table1.getTableHeader().setReorderingAllowed(false);
        table1.setRowHeight(30);
        table1.getColumnModel().getColumn(0).setPreferredWidth(40);
        JScrollPane scrollPane = new JScrollPane(table1);
        table1.setPreferredScrollableViewportSize(new Dimension(400, 420));
        panel3.add(scrollPane);
        Add_Component(jfr,gbl,panel3,gbc,0,2,1,1,1,0);

        //Marks
        String average="Weighted Arithmetic Mean: "+averagenumber;
        String gpa="GPA: "+gpanumber+" (Use BUPT algorithm)";
        String rank="Ranking: 23.1%";

        panel4.setLayout(new BorderLayout());
        panel4.setBorder(BorderFactory.createEmptyBorder());
//        panel5.setLayout(new BorderLayout());
//        panel5.setBorder(BorderFactory.createEmptyBorder());
        JLabel label4 = new JLabel("<html><body>Grades Analysis"+"<br>"+average+"<br>"+gpa+"<br>"+rank+"</body></html>", JLabel.CENTER);
        label4.setFont(new Font("TimesRoman", Font.ITALIC, 25));
//        JLabel label5 = new JLabel(analysis, JLabel.CENTER);
//        label5.setFont(new Font("TimesRoman", Font.ITALIC, 15));
        //JPanel empty3 = new JPanel();
        //panel4.add(empty3, BorderLayout.NORTH);
        panel4.add(label4, BorderLayout.CENTER);
//        panel5.add(label5, BorderLayout.CENTER);
        Add_Component(jfr, gbl, panel4, gbc, 1, 2, 1, 1, 1, 0);
//        Add_Component(jfr, gbl, panel5, gbc, 1, 1, 4, 1, 1, 0);

        panel5.setLayout(new BorderLayout());
        panel5.setBorder(BorderFactory.createEmptyBorder());
        JButton button1 = new JButton(" Update  ");
        JButton button2 = new JButton(" Delete  ");
        button2.setBorder(BorderFactory.createRaisedBevelBorder());
        button2.setBackground(Color.cyan);
        button2.setFont(new Font("STSong", Font.BOLD, 25));
        button1.setBorder(BorderFactory.createRaisedBevelBorder());
        button1.setBackground(Color.cyan);
        button1.setFont(new Font("STSong", Font.BOLD, 25));
        JPanel empty2 = new JPanel();
        empty1.setPreferredSize(new Dimension(100, 10));
        panel5.add(button1, BorderLayout.WEST);
        panel5.add(button2, BorderLayout.EAST);
        panel5.add(empty2, BorderLayout.NORTH);
        Add_Component(jfr, gbl, panel5, gbc, 0, 3, 1, 1, 1, 0);


        button1.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        jfr.setVisible(false);
                        new ModulesAdd(number);
                        jfr.dispose();
                    }
                }
        );

        button2.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        jfr.setVisible(false);
                        new ModulesDelete(number);
                        jfr.dispose();
                    }
                }
        );


    }
}
